package com.myway.service.ibe.command;

import com.myway.dto.logs.DTOIBELogs;
import com.myway.pojo.FlyTickets;
import com.myway.service.ibe.param.RefundFormParam;
import com.myway.service.ibe.param.RefundParam;
import com.travelsky.ibe.client.pnr.*;
import com.travelsky.ibe.exceptions.IBEException;

import java.util.List;

/**
 * Created by yufei on 2018/6/28.
 * 退票
 */
public class RefundTicketCommand extends BaseIBECommand{

    private static String airline = "MJ";

    /**
     * 退票
     * 在E-TERM中填写退票单后票面上该航段会自动refunded，但使用IBE接口需要自己手动去refunded票面。
     *对于往返航段，已经退过一个航段的客票，需要提取该退票单并修改，以保留上次退票信息。
     * @param tickets
     * @throws Exception
     */
    public  void trfd(FlyTickets tickets) throws Exception {// 退票
        //第一步、 REFUNDED票面上的航段状态
        ETRF etrf = new ETRF();
        this.configIBEClient(etrf);
        etrf.refundETkt(tickets.getTicketId(), 2, tickets.getCurrency(), tickets.getFareBasis(), "1", "");
        //第二步、 填写退票单，连接主机
        TRFD trfd = new TRFD();
        this.configIBEClient(trfd);
        TRFDResult result = null;
        //第三步、 生成退票单。先尝试获取退票单，如果没有则新生成一个新退票单。
        try {
            result = trfd.genRefundForm("TM", 2, "I", tickets.getTicketId());
        } catch (IBEException e) {
            if (!e.getMessage().contains("CAN NOT PRINT NEW REFUND")) {// 判断是否已经生成过退票单
                throw e;
            }
            //第四步、 生成新的退票单
            result = trfd.genRefundForm("AM", 2, "I", null);
            result.setTktNo(tickets.getTicketId().split("-")[1]);
            result.setRefund("N");
            result.setPassName("SC");
            result.setAirlineCode(tickets.getTicketId().split("-")[0]);
            result.setCommissionRate(0);
        }
        //第五步、填写/修改退票单信息
        result.setTax(0, 100.00, "CN");
        result.setTax(1, 140.00, "YQ");
        result.setDeduction(22);// 手续费
        result.setGrossRefund(1000.00);
        result.setCouponNo(0, "1200"); // 新票联号
        String s = trfd.changeRefundForm_new(result, false);
        System.out.println(s);
    }

    /**
     * 退票
     */
    public String trfd(RefundParam refundParam) throws Exception {
        ETRF etrf = new ETRF();
        DTOIBELogs ibeETDZLog = new DTOIBELogs();
        int international = 0;
//         intl 未启用
//        if(intl != null && intl != "")
//            international = Integer.valueOf(intl);
        this.configIBEClient(etrf,airline,ibeETDZLog,international);
        String printNo = ibeETDZLog.getPrintNo()!=null&&ibeETDZLog.getPrintNo()!=""?ibeETDZLog.getPrintNo():"2";
        return etrf.refundETkt(refundParam.getTicketNo(),refundParam.getSegId(),refundParam.getCurrency(),refundParam.getAmount(),printNo,"");
    }

    public String refundForm(RefundFormParam refundFormParam) throws Exception {
        TRFD trfd = new TRFD();
        this.configIBEClient(trfd);
        TRFDResult result = null;
        try {
            result = trfd.genRefundForm("TM", 2, "I", refundFormParam.getAirlineCode()+"-"+refundFormParam.getTicketNo());
        } catch (IBEException e) {
            if (!e.getMessage().contains("CAN NOT PRINT NEW REFUND")) {// 判断是否已经生成过退票单
                throw e;
            }
            result = trfd.genRefundForm("AM", 2, "I", null);
            result.setTktNo(refundFormParam.getTicketNo());
            result.setRefund("N");
            result.setPassName(refundFormParam.getPassName());
            result.setAirlineCode(refundFormParam.getAirlineCode());
        }
        result.setCurrency(refundFormParam.getCurrency());
        result.setGrossRefund(refundFormParam.getGrossRefund());
        result.setDeduction(refundFormParam.getDeduction());
        result.setNetRefund(refundFormParam.getNetRefund());
        result.setCouponNo(0,refundFormParam.getCouponNo());// 新票联号
        return trfd.changeRefundForm_new(result, false);
    }

    public String vt(String ticketNo,String pnrNo) throws Exception {
        VT vt = new VT();
        this.configIBEClient(vt,"MJ");
        return vt.voidETicket("2",ticketNo,"MJ",pnrNo);
    }

    public boolean ticketIsUse(String ticketNo) throws Exception{
        DETR detr = new DETR();
        this.configIBEClient(detr, "MJ");
        DETRResult result = detr.getScheduleByTktNo(ticketNo);
        if(result == null) return false;
        DETRSegment segment = result.getSegment(0);
        if(segment == null) return false;
        String ticketStatus = segment.getTicketStatus();
        if(ticketStatus.equals("OPEN FOR USE")) return true;
        return false;
    }
}
